# capital-fabrica.R

# Exemplo de uso:
#
# source('capital-fabrica.R')
# PlotCapitalEmpirico(5, 100, 1.3, 0)
# PlotCapitalEmpirico(5, 100, 1.3, 0.1)
#
# PlotCapitalSimulado(5, 100, 1.3, 0)
# PlotCapitalSimulado(5, 100, 1.3, 0.1)
#
# PlotCapitalSimuladoVarios(5, 100, 1.1, 0

source('dados.R')
source('capital.R')

# T should start at zero

PlotCapital <- function(T, X, u0, c., cadj, r, nanos=0, t0=0, alpha=1, overlap=FALSE, ylim=NULL)
{  # cadj=0 para premio fixo
	if(nanos == 0)
		nanos <- ceiling(T[length(T)])

	#TU <- TestaRuina_PremioFixo(T, X, u0, c., TRUE)
	TU <- TestaRuina_PremioAdj(T, X, u0, c., cadj, nanos, TRUE)

	RuinT <- which(TU$U < 0)

	paint <- plot
	if(length(ylim) == 0)
		ylim <- c(min(0, TU$U), max(TU$U))
	xlim <- c(t0, T[length(T)])
	if(overlap)
		paint <- lines
	color <- 'blue'
	if(alpha != 1) {
		#color <- rgb(0,0,0,alpha)
		if(length(RuinT) > 0)
			color <- rgb(1,0,0,alpha)
		else
			color <- rgb(0,0,1,alpha)
	}
	paint(TU$T+t0, TU$U, type='l',
		main=paste('U(t) = u + c*t - X ,',
			'u:', format(u0, digits=3),
			'c:', format(c., digits=3)),
			#', para r:', format(r, digits=5)),
		cex.main = .9, cex.lab = .8,
		col=color, lwd=2,
		xlim = xlim, ylim=ylim,
		xlab='years', ylab='capital (in billions of dollars)')

	if(alpha == 1 && length(RuinT) > 0) {
		t <- TU$T[RuinT[1]] + t0
		lines(c(t, t), ylim,
			col='#ff00005f')
	}
	if(alpha == 1)
		points(T+t0, rep(min(c(0, ylim[1])), length(T)), col='black', pch='x', cex=.75)
	return(length(RuinT) == 0)
}

PlotCapitalEmpirico <- function(r, u0, ccoef, cadj) {
	T <- Anos(r)
	t0 <- floor(T[1])
	X <- Danos(r)
	c. <- Ccoef(T, X, ccoef)
	PlotCapital(T-t0, X, u0, c., cadj, r, t0=t0)
}

PlotCapitalEmpiricoMeio <- function(r, u0, ccoef, cadj) {
	T <- Anos(r)
	t0 <- floor(T[1])
	X <- Danos(r)
	c. <- Ccoef(T[1:(length(T)/2)], X, ccoef)
	PlotCapital(T-t0, X, u0, c., cadj, r, t0=t0)
}

PlotCapitalEmpiricoExt <- function(r, u0, ccoef, cadj) {
	c. <- Ccoef(Anos(r), Danos(r), ccoef)
	T <- Anos(r, .anos2, .danos2)
	t0 <- floor(T[1])
	X <- Danos(r, .danos2)
	PlotCapital(T-t0, X, u0, c., cadj, r, t0=t0)
}

PlotCapitalSimulado <- function(r, u0, ccoef, cadj) {
	T <- Anos(r)
	X <- Danos(r)
	c. <- Ccoef(T, X, ccoef)
	TX <- SimulaGamma(NAnos(T), Danos(r))
	PlotCapital(TX$T, TX$X, u0, c., cadj, r, nanos=NAnos(T))
}



#####




PlotCapital. <- function(T, X, u0, c., cadj, r, alpha=1, overlap=FALSE, ylim=NULL)
{  # cadj=0 para premio fixo
	t0 <- T[1]
	T <- T - t0  # começar em 0

	nanos <- ceiling(T[length(T)])

	#TU <- TestaRuina_PremioFixo(T, X, u0, c., TRUE)
	TU <- TestaRuina_PremioAdj(T, X, u0, c., cadj, nanos, TRUE)

	xlim <- c(t0, T[length(T)])
	RuinT <- which(TU$U < 0)
#	if(length(RuinT) > 0 && alpha < 1)  # only some
#		TU <- data.frame(U = TU$U[1:RuinT[1]], T = TU$T[1:RuinT[1]])

	paint <- plot
	if(length(ylim) == 0)
		ylim <- c(min(0, TU$U), max(TU$U))
	if(overlap)
		paint <- lines
	color <- 'blue'
	if(alpha != 1) {
		#color <- rgb(0,0,0,alpha)
		if(length(RuinT) > 0)
			color <- rgb(1,0,0,alpha)
		else
			color <- rgb(0,0,1,alpha)
	}
	paint(TU$T+t0, TU$U, type='l',
		main=paste('U(t) = u + c*t - X , em que',
			'u:', format(u0, digits=3),
			'c:', format(c., digits=3),
			', para r:', format(r, digits=5)),
		cex.main = .9, cex.lab = .8,
		col=color, lwd=2,
		xlim=xlim, ylim=ylim,
		xlab='anos', ylab='capital (mil milhões de dólares)')

	if(alpha == 1 && length(RuinT) > 0) {
		t <- TU$T[RuinT[1]] + t0
		lines(c(t, t), ylim,
			col='#ff00005f')
	}
	if(alpha == 1)
		points(T+t0, rep(min(c(0, ylim[1])), length(T)), col='black', pch='x', cex=.75)
	return(length(RuinT) == 0)
}

PlotCapitalSimuladoVarios <- function(r, u0, ccoef, cadj) {
	T <- Anos(r)
	X <- Danos(r)
	c. <- Ccoef(T, X, ccoef)
	ylim <- c(-1000, 5000)
	nruin <- 0
	nsim <- 100
	nanos <- 100
	for(i in 1:nsim) {
		TX <- SimulaGamma(nanos, Danos(r))
		if(!PlotCapital(TX$T, TX$X, u0, c., cadj, r, alpha=0.10, overlap=i!=1, ylim=ylim))
			nruin <- nruin+1
	}

	ruins <- nruin/nsim
	lines(c(0, nanos), c(0,0), col='red', lwd=2)
	text(nanos-5, +50, paste((1-ruins)*100, '%'), c(1, 0))
	text(nanos-5, -50, paste(ruins*100, '%'), c(1, 1))
}

